{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
~}}

<Secrets::DestinationHeader @destination={{@destination}} />

{{#if @associations.meta.filteredTotal}}
  <div class="has-bottom-margin-s">
    {{#each @associations as |association index|}}
      <ListItem as |Item|>
        <Item.content>
          <div>
            <Hds::Badge @text="{{association.mount}}/" />
            <LinkToExternal
              data-test-association-name={{index}}
              class="has-text-black has-text-weight-semibold"
              @route="kvSecretDetails"
              @models={{array association.mount association.secretName}}
            >
              {{association.secretName}}
            </LinkToExternal>
            <div>
              <SyncStatusBadge @status={{association.syncStatus}} data-test-association-status={{index}} />
              <code class="has-text-grey is-size-8" data-test-association-updated={{index}}>
                last updated on
                {{date-format association.updatedAt "MMMM do yyyy, h:mm:ss a"}}
              </code>
            </div>
          </div>
        </Item.content>

        <Item.menu>
          {{#if (or association.setAssociationPath.isLoading association.removeAssociationPath.isLoading)}}
            <li class="action">
              <LoadingDropdownOption />
            </li>
          {{else}}
            <li class="is-inline-block">
              <Hds::Button
                @text="Sync now"
                class="link"
                disabled={{not association.canSync}}
                data-test-association-action="sync"
                {{on "click" (fn this.update association "set")}}
              />
            </li>
            <li>
              <LinkToExternal
                class="has-text-black has-text-weight-semibold"
                data-test-association-action="view"
                @route="kvSecretDetails"
                @models={{array association.mount association.secretName}}
              >
                View secret
              </LinkToExternal>
            </li>
            {{#if association.canUnsync}}
              <ConfirmAction
                data-test-association-action="unsync"
                @isInDropdown={{true}}
                @buttonText="Unsync"
                @confirmMessage="This secret will be unsynced from all destinations."
                @onConfirmAction={{fn this.update association "remove"}}
              />
            {{/if}}
          {{/if}}
        </Item.menu>
      </ListItem>
    {{/each}}
    <Hds::Pagination::Numbered
      @currentPage={{@associations.meta.currentPage}}
      @currentPageSize={{@associations.meta.pageSize}}
      @route="secrets.destinations.destination.secrets"
      @showSizeSelector={{false}}
      @totalItems={{@associations.meta.filteredTotal}}
      @queryFunction={{this.paginationQueryParams}}
      data-test-pagination
    />
  </div>
{{else}}
  <EmptyState
    @title="No synced secrets yet"
    @message="Select secrets from existing KV version 2 engines and sync them to the destination."
  >
    <LinkTo class="has-top-margin-xs" @route="secrets.destinations.destination.sync">
      Sync secrets
    </LinkTo>
  </EmptyState>
{{/if}}